package com.liu.qqRobot.wm.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.liu.common.utils.QQClientUtils;
import com.liu.mybatisPlus.q.service.QGroupUserOperationQuantityService;
import com.liu.mybatisPlus.wm.entity.base.WmRivenListen;
import com.liu.mybatisPlus.wm.service.WmRivenListenService;
import com.liu.qqRobot.event.entity.dto.EventReportingDTO;
import com.liu.qqRobot.q.enums.OperationTypeEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class wmrldHandler {

    @Resource
    private WmRivenListenService wmRivenListenService;

    @Resource
    private QGroupUserOperationQuantityService qGroupUserOperationQuantityService;

    /**
     * 处理事件
     * @param data 信息内容
     * @param groupId 群号
     * @param userId 发送人QQ号
     */
    public void handler(EventReportingDTO.Message.Data data, Long groupId, Long userId, Boolean isGroup) {
        if (data.getText().replace("wmrld ", "").equals("删除任务")) {
            QueryWrapper<WmRivenListen> wmRivenListenQueryWrapper = new QueryWrapper<>();
            wmRivenListenQueryWrapper.eq("user_id", userId);
            wmRivenListenQueryWrapper.eq("group_id", groupId);
            wmRivenListenQueryWrapper.eq("is_delete", false);
            List<WmRivenListen> wmRivenListenList = wmRivenListenService.list(wmRivenListenQueryWrapper);
            wmRivenListenList.forEach(wmRivenListen -> {
                if (wmRivenListen != null) {
                    UpdateWrapper<WmRivenListen> wmRivenListenUpdateWrapper = new UpdateWrapper<>();
                    wmRivenListenUpdateWrapper.eq("user_id", userId);
                    wmRivenListenUpdateWrapper.eq("group_id", groupId);
                    wmRivenListenUpdateWrapper.eq("is_delete", false);
                    wmRivenListenUpdateWrapper.set("is_delete", true);
                    wmRivenListenService.update(wmRivenListenUpdateWrapper);
                    QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n删除任务成功，参数：" + wmRivenListen.getParam(), userId, groupId, isGroup);
                    qGroupUserOperationQuantityService.statisticsOperationQuantityByType(groupId, userId, OperationTypeEnum.WMRLD);
                } else {
                    QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n你在本群还没有任务", userId, groupId, isGroup);
                }
            });
        }
    }
}
